<?php defined('SYSPATH') or die('No direct script access.');
	
	class Model_Services_Surgery extends Model implements Jqgrid_GridListener{
	
		public $surgery;
		
		public function count($criteria = false){
			return ORM::factory("dao_surgery")->count_all();
		}
			
		public function getList($criteria = false){
			
			if (!$criteria)	$surgery_list = ORM::factory("dao_surgery")->find_all()->as_array();
			else{

				if ($criteria->isSearch){
					$surgery_list = ORM::factory("dao_surgery")
									->where($criteria->getSQLField("surgeries"),$criteria->getSQLOper(), $criteria->getSQLSearchString())
									->order_by($criteria->sidx,$criteria->sord)
									->limit($criteria->limit)
									->offset($criteria->calcOffset())
									->find_all()
									->as_array();
							
				}else{
					$surgery_list = ORM::factory("dao_surgery")->order_by($criteria->sidx,$criteria->sord)->limit($criteria->limit)->offset($criteria->calcOffset())->find_all()->as_array(); 
				}
				
				
			}
			 
				
			$dataset = array();
			foreach ($surgery_list as $surgery){
				$dataset[$surgery->id] = array($surgery->surgery_name,$surgery->description);
			}
			
			return $dataset;
		}
		
		/**
		*
		* get all surgeries from an id
		* @param int $minId
		* @param int $maxAmount
		* @return array of surgery
		*/
		public function getListFromMinID($minId, $maxAmount){
			return ORM::factory("dao_surgery")->where("id",">",$minId)->order_by("id", "ASC")->limit($maxAmount)->find_all()->as_array();
		}
	
		public function get($id){
			return ORM::factory("dao_surgery")->find($id);
		}
		
		public function getAll(){
			return ORM::factory("dao_surgery")->find_all()->as_array();
		}
		
		public function save($id, array $data){
			$this->surgery = ($id > 0) ? ORM::factory("dao_surgery",$id) : ORM::factory("dao_surgery");
			$this->surgery->surgery_name = $data['surgery_name'];
			$this->surgery->description = $data['description'];
			$this->surgery->save();
			return $this->surgery->id;
	    }
		
		public function delete($id){
			$surgery = ORM::factory("dao_surgery",$id);
			$surgery->delete();
			return $id;
		}
		
		
		/**
		*
		* return an entity surgery by name
		* @param string $name
		* @return surgery
		*/
		public function getByName($name){
			return ORM::factory("dao_surgery")->where("surgery_name","=",$name)->find();
		}
		
	}



?>